<?php


namespace app\tickets\controller;


use app\api\controller\Commondevice;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Checked extends Base
{
    /**
     * 统计设备
     * @return mixed
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function index(){
        $where['id'] = ['gt',0];
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['serial'] = ['like',"%{$keyword}%"];
        }
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("tickets_device")->limit($start, $limit)->where($where)
                    ->order("id desc")->select();
                foreach ($list as $k => $v) {
                    $list[$k]['addtime'] = date("Y-m-d H:i:s", $v['addtime']);
                    $list[$k]['status_name'] = empty($v['status'])?"离线":"在线";
                }
                $count = Db::name("tickets_device")->limit($start, $limit)->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch('index');
    }

    /**
     * @return mixed
     * @author zhixuedong
     * @date 2021/2/1/001
     */
    public function log(){
        $where['id'] = ['gt',0];
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("tickets_device_log")->limit($start, $limit)->where($where)
                    ->order("id desc")->select();
                $count = Db::name("tickets_device_log")->limit($start, $limit)->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch('log');
    }

    /**
     * 注册设备视图
     * @author zhixuedong
     * @date 2021/2/2/002
     */
    public function addDevice(){
        return view('create');
    }

    /**
     * 注册二维码设备
     * @return mixed
     * @author zhixuedong
     * @date 2021/2/2/002
     */
    public function doDevice(){
        try{
            $rule = [
                ["deviceSerial", "require", "设备序列号不能为空"],
                ["qrcodeSerial", "require", "二维码设备序列号不能为空"],
                ["validateCode", "require", "设备验证码不能为空"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            $commondevice = new Commondevice();
            $deviceAdd = $commondevice->deviceAdd($value);
            if($deviceAdd!==true){
                throw new Exception($deviceAdd['msg']);
            }
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    /**
     * 删除设备
     * @author zhixuedong
     * @date 2021/2/2/002
     */
    public function delDevice(){
        try{
            $detail = Db::name("tickets_device")->where(['id'=>$_POST['id']])->find();
            $commondevice = new Commondevice();
            $commondevice->deviceDel($detail['serial']);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
            Db::commit();
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            Db::rollback();
        }
        return $black;
    }

}